import 'package:thinking/entity/_.dart';
import 'package:thinking/db/SQLite.dart';

class ClassService{

  /// 向指定表插入List数据
  static Future<void> addList(String? tableName, List<ClassData>? res) async {
    List<Map<String, dynamic>> data = await ClassService.findAll(tableName!);
    if (data.isEmpty) {
      var _db = await SQLite().db;
      res!.forEach((e) async {
        await _db!.insert(tableName, e.toJson());
      });
    }
  }


  /// 查询指定表中菜单数据
  static Future<List<Map<String,dynamic>>> findAll(String tableName) async{

      var _db = await SQLite().db;

      List<Map<String,dynamic>> result = await _db!.query(tableName);
      return result.isNotEmpty
          ? result
          :[];

  }


  /// 添加菜单到指定表
  static Future<int> add({ String? tableName , ClassData? item }) async {
    var _db = await SQLite().db;
    return await _db!.insert(tableName!, item!.toJson());
  }

  /// 从指定表删除菜单
  static Future<int> delete({ String? tableName , ClassData? item }) async {
    var _db =  await SQLite().db;
    return await _db!.delete(
        tableName!,
        where: 'id = ?',
        whereArgs: [item!.id]
    );
  }

}

